package com.foreknow.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Disecategory;
import com.foreknow.service.impl.DisecategoryServiceImpl;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/disease")
public class DisecategoryController {
    @Autowired
    private DisecategoryServiceImpl disecategoryService;

    @RequestMapping(value = "/findDisecategoryByPage", method = RequestMethod.POST)
    public String findUserByPage(@RequestBody Map<String, Object> para) throws JsonProcessingException {
        System.out.println(para);
        int pageNum = (int) para.get("pagenum");
        int pageSize = (int) para.get("pagesize");
        PageHelper.startPage(pageNum, pageSize);
        List<Disecategory> disecategories = disecategoryService.selectAllDisecategory();
        HashMap<String, Object> hs = new HashMap<>();
        if (disecategories != null) {
            //PageInfo是PageHelper中内置的分页信息类
            PageInfo<Disecategory> info = new PageInfo(disecategories);
            hs.put("data", info.getList());
            hs.put("meta", new Constance("msg", 200));
            hs.put("total", info.getTotal());
        } else {
            hs.put("meta", new Constance("获取列表失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/addCategory", method = RequestMethod.POST)
    public String addUser(@RequestBody Map<String, Object> para) throws JsonProcessingException {
        Disecategory disecategory = new Disecategory();
        String dicacode = (String) para.get("dicacode");
        String dicaname = (String) para.get("dicaname");
        String DicaType = (String) para.get("dicatype");
        String sequenceno = (String) para.get("sequenceno");
        disecategory.setSequenceno(Integer.parseInt(sequenceno));
        disecategory.setDicacode(dicacode);
        disecategory.setDicatype(Integer.parseInt(DicaType));
        disecategory.setDicaname(dicaname);
        int isRight = disecategoryService.addCategory(disecategory);
        HashMap<String, Object> hs = new HashMap<>();
        if (isRight == 1) {
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("新增分类失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/deleteCategory", method = RequestMethod.DELETE)
    private String delete(@RequestParam Map<String, Object> para) throws JsonProcessingException {
        String id = (String) para.get("id");
        int isRight = disecategoryService.deleteById(Integer.parseInt(id));
        HashMap<String, Object> hs = new HashMap<>();
        if (isRight == 1) {
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("删除失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/selectCategoryOne", method = RequestMethod.GET)
    public String selectOne(@RequestParam Map<String, Object> para) throws JsonProcessingException {
        String id = (String) para.get("id");
        Disecategory d = disecategoryService.selectOne(Integer.parseInt(id));
        HashMap<String, Object> hs = new HashMap<>();
        if (d != null) {
            hs.put("meta", new Constance("msg", 200));
            hs.put("data", d);
        } else {
            hs.put("meta", new Constance("查询失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/updatetCategory", method = RequestMethod.POST)
    public String update(@RequestBody Map<String, Object> para) throws JsonProcessingException {
        Disecategory disecategory = new Disecategory();
        int id = (int) para.get("id");
        String dicacode = (String) para.get("dicacode");
        String dicaname = (String) para.get("dicaname");
        String DicaType = (String) para.get("dicatype");
        int sequenceno = (int) para.get("sequenceno");
        disecategory.setId(id);
        disecategory.setSequenceno(sequenceno);
        disecategory.setDicacode(dicacode);
        disecategory.setDicatype(Integer.parseInt(DicaType));
        disecategory.setDicaname(dicaname);
        int isRight = disecategoryService.update(disecategory);
        HashMap<String, Object> hs = new HashMap<>();
        if (isRight == 1) {
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("修改分类信息失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
}